package com.ss.android.video.a;

import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.dispatcher.AbsRequest;
import com.ss.android.article.base.app.AppData;

import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public final class VideoDiagnosis extends AbsRequest {
    private static ArrayList h = new ArrayList();
    private String videoId;
    private String videoUrl;
    private String errTip;
    private String callBackErrorStr;
    private String header;
    private String data;
    private JSONObject statisticsObj;

    public VideoDiagnosis(String videoId,
                          String videoUrl,
                          String errTip,
                          String callBackErrorStr,
                          JSONObject statisticsObj) {
        super("video_diagnosis", Priority.LOW);
        this.videoId = videoId;
        this.videoUrl = videoUrl;
        this.errTip = errTip;
        this.callBackErrorStr = callBackErrorStr;
        this.statisticsObj = statisticsObj;
    }

    private static String getPlayerLog() {
        String v0;
        if (h.size() == 0) {
            v0 = "";
        } else {
            StringBuffer v2 = new StringBuffer();
            int v1;
            for (v1 = 0; v1 < h.size(); ++v1) {
                v2.append(h.get(v1) + "\n");
            }
            h.clear();
            v0 = v2.toString();
        }

        return v0;
    }

    public static void a(String videoId) {
        if (AppData.getInstance().getAbSettings().isVideoDiagnosisEnabled()) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("error_type", "type_api");
                jsonObject.put("videoId", videoId);
            } catch (Throwable throwable) {
            }

            com.bytedance.article.common.a.g.a("video_diagnosis", jsonObject);
            Logger.d(jsonObject.toString());
        }
    }

    public static void performVideoDiagnosis(String videoId) {
        if (AppData.getInstance().getAbSettings().isVideoDiagnosisEnabled()) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("error_type", "type_no_response");
                if (!TextUtils.isEmpty(videoId)) {
                    jsonObject.put("videoId", videoId);
                }

                String playerLog = getPlayerLog();
                if (!TextUtils.isEmpty(playerLog)) {
                    jsonObject.put("playerlog", playerLog);
                }
            } catch (Throwable throwable) {
            }
            com.bytedance.article.common.a.g.a("video_diagnosis", jsonObject);
            Logger.d(jsonObject.toString());
        }
    }

    public static void callbackOnError(String arg4) {
        if ((AppData.getInstance().getAbSettings().isVideoDiagnosisEnabled()) && !TextUtils.isEmpty(((CharSequence) arg4))) {
            if (h.size() > 10) {
                h.remove(0);
            }
            h.add(System.currentTimeMillis() + " " + arg4);
        }
    }

    private void a(URLConnection arg6) {
        if (arg6 != null) {
            StringBuilder v2 = new StringBuilder("");
            Iterator v3 = arg6.getHeaderFields().entrySet().iterator();
            while (v3.hasNext()) {
                Object v0 = v3.next();
                v2.append(((Map.Entry) v0).getKey() + ":" + ((Map.Entry) v0).getValue() + "\n");
            }

            this.header = v2.toString();
        }
    }

    public final void run() {
        JSONObject v1_3;
        URLConnection v0_1;
        long v4 = 0;
        super.run();
        if (TextUtils.isEmpty(this.videoId)) {
            return;
        }

        if (TextUtils.isEmpty(this.videoUrl)) {
            return;
        }

        URLConnection v1 = null;
        try {
            v0_1 = new URL(this.videoUrl).openConnection();
            if (v4 > v4) {
                ((HttpURLConnection) v0_1).setRequestProperty("Range", "bytes=0-1000");
            }

            ((HttpURLConnection) v0_1).setConnectTimeout(50000);
            ((HttpURLConnection) v0_1).setReadTimeout(50000);
            ((HttpURLConnection) v0_1).setUseCaches(false);
            ((HttpURLConnection) v0_1).setInstanceFollowRedirects(true);
            ((HttpURLConnection) v0_1).getResponseCode();
            if (v0_1 != null) {
                this.a(v0_1);
                this.data = "null";
                byte[] v1_2 = new byte[1000];
                int v2 = v0_1.getInputStream().read(v1_2, 0, 1000);
                if (v2 <= 0) {
                } else {
                    this.data = com.bytedance.common.utility.aa.a(v1_2, v2);
                }
                v1_3 = new JSONObject();
                v1_3.put("error_type", "type_player");
                String playerlog = getPlayerLog();
                if (!TextUtils.isEmpty(((CharSequence) playerlog))) {
                    v1_3.put("playerlog", playerlog);
                }

                if (!TextUtils.isEmpty(this.videoId)) {
                    v1_3.put("videoId", this.videoId);
                }

                if (!TextUtils.isEmpty(this.videoUrl)) {
                    v1_3.put("videoUrl", this.videoUrl);
                }

                if (!TextUtils.isEmpty(this.errTip)) {
                    v1_3.put("errTip", this.errTip);
                }

                if (!TextUtils.isEmpty(this.callBackErrorStr)) {
                    v1_3.put("callBackErrorStr", this.callBackErrorStr);
                }

                if (!TextUtils.isEmpty(this.header)) {
                    v1_3.put("header", this.header);
                }

                if (!TextUtils.isEmpty(this.data)) {
                    v1_3.put("data", this.data);
                }

                if (this.statisticsObj == null) {
                } else {
                    v1_3.put("statisticsObj", this.statisticsObj);
                }
                com.bytedance.article.common.a.g.a("video_diagnosis", v1_3);
                Logger.d(v1_3.toString());
            }
        } catch (Throwable v1_1) {
            Logger.ee(v1_1.getMessage());
        } finally {
            if (v1 != null) {
                try {
                    ((HttpURLConnection) v1).disconnect();
                } catch (Throwable v1_1) {
                }
            }
        }
    }
}

